package com.lovo.market.common.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

/**
 * @author MirAi
 * @description 模型转换工具类
 * @date 2020/12/15 17:12
 */
@Slf4j
public final class PojoTransferUtil {

    /**
     * 复制属性
     *
     * @param srcObj 源对象
     * @param targetClass 目标模板
     * @return 得到对应属性的对象
     */
    public static <Target> Target transfer(Object srcObj, Class<Target> targetClass) {

        if (srcObj == null || targetClass == null) {
            return null;
        }

        try {

            Target newInstance = targetClass.newInstance();
            BeanUtils.copyProperties(srcObj, newInstance);
            return newInstance;

        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

        return null;
    }

}
